﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Net;

namespace LongSpeed.WebSite.UserControl
{
    public partial class ucQueryDomain : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Visible = false;
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (TextBox1.Value == "")
            {
               // X.Msg.Alert("警告！", "请输入需要查询域名！").Show();
            }
            else
            {
                Label1.Text = "查询结果：<br/>";
                for (int i = 0; i < CheckBoxList1.Items.Count; i++)
                {
                    if (CheckBoxList1.Items[i].Selected == true)
                    {
                        string a = "";
                        string b = "";
                        Encoding gb2312 = Encoding.GetEncoding("gb2312");
                        if (regzw(TextBox1.Value.Trim().ToString()))
                        {
                            a = HttpUtility.UrlEncode(TextBox1.Value.Trim().ToString(), System.Text.ASCIIEncoding.GetEncoding("gb2312")).ToString();
                        }
                        else
                        {
                            a = TextBox1.Value.Trim().ToString();
                        }
                        if (regzw(CheckBoxList1.Items[i].Text.ToString().Replace(".", "")))
                        {
                            b = HttpUtility.UrlEncode(CheckBoxList1.Items[i].Text.ToString(), System.Text.ASCIIEncoding.GetEncoding("gb2312")).ToString();
                        }
                        else
                        {
                            b = CheckBoxList1.Items[i].Text;
                        }
                        WebRequest req = WebRequest.Create("http://panda.www.net.cn/cgi-bin/check.cgi?area_domain=" + a + b);
                        WebResponse res = req.GetResponse();
                        Stream ReceiveStream = res.GetResponseStream();
                        StreamReader sr = new StreamReader(ReceiveStream);
                        string resultstring = sr.ReadToEnd();
                        resultstring = HttpUtility.UrlDecode(resultstring, System.Text.ASCIIEncoding.GetEncoding("gb2312")).ToString();
                        resultstring = resultstring.Replace("200", "");
                        resultstring = resultstring.Replace("300", "");
                        resultstring = resultstring.Replace("210", "<span style='color:green;'>域名可以注册！</span>");
                        resultstring = resultstring.Replace("211", "<span style='color:red;'>域名已经注册！</span>");
                        resultstring = resultstring.Replace("212", "<span style='color:red;'>域名参数传输错误！</span>");
                        resultstring = resultstring.Replace("213", "<span style='color:red;'>域名查询超时！</span>");
                        resultstring = resultstring.Replace(": Domain name is available", "");
                        resultstring = resultstring.Replace(": Domain name is not available", "");
                        resultstring = resultstring.Replace(": Domain name is invalid", "");
                        resultstring = resultstring.Replace(": Timeout", "");
                        Label1.Text = Label1.Text + resultstring + "<br/>";
                        Label1.Visible = true;
                    }
                }
            }
        }

        public static bool regzw(string inputtxt)
        {
            Regex rg = new Regex(@"^[\u4e00-\u9fa5]*\s*$");
            Match mc = rg.Match(inputtxt);
            return mc.Success;
        }

        protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (RadioButtonList1.SelectedValue == "英文域名")
            {
                for (int i = 0; i < CheckBoxList1.Items.Count; i++)
                {
                    CheckBoxList1.Items[i].Enabled = true;
                    CheckBoxList1.Items[i].Selected = false;
                }
                CheckBoxList1.Items[18].Enabled = false;
                CheckBoxList1.Items[19].Enabled = false;
                CheckBoxList1.Items[20].Enabled = false;
            }
            if (RadioButtonList1.SelectedValue == "中文域名")
            {
                for (int i = 0; i < CheckBoxList1.Items.Count; i++)
                {
                    CheckBoxList1.Items[i].Enabled = false;
                    CheckBoxList1.Items[i].Selected = false;
                }
                CheckBoxList1.Items[0].Enabled = true;
                CheckBoxList1.Items[2].Enabled = true;
                CheckBoxList1.Items[5].Enabled = true;
                CheckBoxList1.Items[8].Enabled = true;
                CheckBoxList1.Items[9].Enabled = true;
                CheckBoxList1.Items[18].Enabled = true;
                CheckBoxList1.Items[19].Enabled = true;
                CheckBoxList1.Items[20].Enabled = true;
            }
        }
    }
}